home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 23 / CU Amiga - Super CD-ROM 23 (June 1998).iso / CUCD / Programming / OUI / cycle.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  3.4 KB  |  142 lines

  1. // $Id: cycle.cc 1.3 1998/01/13 19:58:14 dlorre Exp $
  2. #include <exec/lists.h>
  3. #include <libraries/gadtools.h>
  4. #include <string.h>
  5.  
  6. #include "gadgets/cycle.h"
  7. #include "gadgetlist.h"
  8.  
  9. #include <proto/gadtools.h>
  10. #include <proto/utility.h>
  11.  
  12. // ========================================================================
  13. // ==========================  CYCLE CLASS ================================
  14. // ========================================================================
  15.  
  16. cycle::cycle(gadgetlist *gl,
  17.              void (window::*func)(gadget *, unsigned long, unsigned short),
  18.              TagItem *tags) :
  19.              gadget(gl, func), labsize(1), cylabs(NULL)
  20. {
  21.     init(tags) ;
  22. }
  23. cycle::cycle(gadgetlist *gl,
  24.              void (window::*func)(gadget *, unsigned long, unsigned short),
  25.              ULONG tag1, ...) :
  26.              gadget(gl, func), labsize(1), cylabs(NULL)
  27. {
  28.     init((TagItem *)&tag1) ;
  29. }
  30.  
  31. void cycle::init(TagItem *tags)
  32. {
  33. const char *label ;
  34.  
  35.     cursel = (LONG)GetTagData(OCYCLE_Active, 0, tags) ;
  36.     label = (const char *)GetTagData(OCYCLE_Label, NULL, tags) ;
  37.     glist->ng->ng_Flags = GetTagData(OCYCLE_Flags, PLACETEXT_LEFT, tags) ;
  38.     initlabs(tags) ;
  39.     glist->ng->ng_GadgetText = (UBYTE *)label ;
  40.     underkey(label) ;
  41.     gad = glist->gad = CreateGadget(CYCLE_KIND, glist->gad, glist->ng,
  42.         GTCY_Labels, cylabs ? cylabs : xlabs,
  43.         GTCY_Active, cursel,
  44.         GT_Underscore,  '_',
  45.         TAG_END) ;
  46. }
  47.  
  48. void cycle::initlabs(TagItem *tags)
  49. {
  50. const char **p ;
  51. const char *cp ;
  52. int i = 0 ;
  53. TagItem *t, *tl ;
  54.  
  55.     labsize = 1 ;
  56.     xlabs = NULL ;
  57.  
  58.     if (FindTagItem(OCYCLE_TextArray, tags)) {
  59.         xlabs = p = (const char **)GetTagData(OCYCLE_TextArray, NULL, tags) ;
  60.         cp = *p++ ;
  61.         while (cp) {
  62.             labsize++ ;
  63.             cp = *p++ ;
  64.         }
  65.      }
  66.     else {
  67.         tl = tags ;
  68.         while (t = NextTagItem(&tl)) {
  69.             if (t->ti_Tag == OCYCLE_Text) {
  70.                 labsize++ ;
  71.             }
  72.         }
  73.         cylabs = new STRPTR[labsize] ;
  74.         tl = tags ;
  75.         while (t = NextTagItem(&tl)) {
  76.             if (t->ti_Tag == OCYCLE_Text) {
  77.                 cp = (const char *)GetTagData(OCYCLE_Text, NULL, t) ;
  78.                 cylabs[i] = new char[strlen(cp)+1] ;
  79.                 strcpy(cylabs[i++], cp) ;
  80.             }
  81.         }
  82.         cylabs[i] = NULL ;
  83.     }
  84. }
  85.  
  86. void cycle::freelabs()
  87. {
  88.     if (cylabs) {
  89.     int i ;
  90.         for ( i=0 ; i<labsize; i++) if (cylabs[i]) delete cylabs[i] ;
  91.         delete cylabs ;
  92.         cylabs = NULL ;
  93.     }
  94. }
  95.  
  96. cycle::~cycle()
  97. {
  98.     freelabs() ;
  99. }
  100. void cycle::action(unsigned long classe, unsigned short code)
  101. {
  102.     cursel = code ;
  103.     gadget::action(classe, code) ;
  104. }
  105.  
  106.  
  107. void cycle::keystroke(BOOL shifted)
  108. {
  109.     if (shifted) {
  110.         cursel-- ;
  111.         if (cursel < 0) cursel=labsize-2 ;
  112.     }
  113.     else {
  114.         cursel++ ;
  115.         if (cursel >= labsize-1)
  116.             cursel = 0 ;
  117.     }
  118.     GT_SetGadgetAttrs(gad, w, NULL,
  119.         GTCY_Active,    cursel,
  120.         TAG_DONE) ;
  121.     gadget::action(NULL, cursel) ;
  122. }
  123.  
  124.  
  125. void cycle::set(TagItem *tags)
  126. {
  127.     cursel = (LONG)GetTagData(OCYCLE_Active, cursel, tags) ;
  128.     if (FindTagItem(OCYCLE_Text, tags) || FindTagItem(OCYCLE_TextArray, tags)) {
  129.         freelabs() ;
  130.         initlabs(tags) ;
  131.     }
  132.     GT_SetGadgetAttrs(gad, w, NULL,
  133.         GTCY_Active,   cursel,
  134.         GTCY_Labels,   cylabs ? cylabs : xlabs,
  135.         TAG_DONE) ;
  136. }
  137.  
  138. void cycle::set(ULONG tag1, ...)
  139. {
  140.     set((TagItem *)&tag1) ;
  141. }
  142.